package GameWorld;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Arrays;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    public static final int RUNNING = 0;//运行状态
    public static final int GAME_OVER=1;//游戏结束状态
    public static int state = RUNNING;//窗口当前状态


    private BattleShip ship = new BattleShip();
    private SeaObject[] submarines = {};
    private Mine[] mines = {};
    private Bomb[] bombs = {};

    //重写paint()画 g:笔
    public void paint(Graphics g){//画潜艇
        System.out.println("ss");
        switch(state){ //根据当前状态做不同的处理
            case GAME_OVER: //游戏结束状态时，画游戏结束图
                Images.gameover.paintIcon(null,g,0,0);
                break;
            case RUNNING: //运行状态时，画海洋图、对象、分和命
                Images.sea.paintIcon(null,g,0,0); //画海洋图
                ship.painImage(g); //画战舰
                for(int i=0;i<submarines.length;i++){ //遍历潜艇数组
                    submarines[i].painImage(g); //画潜艇
                    System.out.println("aaa");
                }
                for(int i=0;i<mines.length;i++){
                    mines[i].painImage(g);
                }
                for(int i=0;i<bombs.length;i++){
                    bombs[i].painImage(g);
                }
                g.drawString("SCORE: "+score,200,50); //画分------------不要求掌握
                g.drawString("LIFE: "+ship.getLife(),400,50); //画命----不要求掌握
        }
    }

    public SeaObject nextSubmarine(){
        System.out.println("生成潜艇");
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return  new ObserveSubmarine();
        }else if (type<15){
            return  new TorpedoSumbmarine();
        }else{
            return new MineSubmarine();
        }
    }


    private int subEnterIndex = 0;
    public void submarineEnterAction(){
        subEnterIndex++;
        if(subEnterIndex%40==0){
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1]=obj;
            System.out.println("submarineEnterAction");
        }
    }


    private int mineEnterIndex= 0;
    //水雷入场
    public void mineEnterAction(){
            mineEnterIndex++;
            if(mineEnterIndex%100==0){
                for (int i=0;i<submarines.length;i++){
                    if (submarines[i] instanceof MineSubmarine) {
                        MineSubmarine ms = (MineSubmarine) submarines[i];
                        Mine obj =  ms.shootMine();
                        mines = Arrays.copyOf(mines,mines.length+1);
                        mines[mines.length-1] = obj;
                    }
                }
            }
    }



    public void moveAction(){
        for (int i=0;i<submarines.length;i++){//遍历所有潜艇
            submarines[i].move();
            System.out.println("moveAction");
        }
        for (int i=0;i< mines.length;i++){//遍历所有水雷
            mines[i].move();
        }
        for (int i=0;i<bombs.length;i++){//遍历所有深水炸弹
            bombs[i].move();
        }
    }



    public void outOfBoundsAction(){
        for (int i=0;i<submarines.length;i++){//遍历所有潜艇
            if (submarines[i].isOutofBounds() || submarines[i].isDead()){//如果越界了
                submarines[i] = submarines[submarines.length-1];//把最后一位元素的数据传给越界的元素中
                submarines = Arrays.copyOf(submarines,submarines.length-1);//把最后一位元素删除
                System.out.println("outOfBoundsAction");
            }

        }
        for (int i=0;i<mines.length;i++){//遍历所有水雷
            if (mines[i].isOutofBounds() || mines[i].isDead()){
                mines[i] = mines[mines.length-1];
                mines = Arrays.copyOf(mines,mines.length-1);
            }
        }
        for (int i=0;i<bombs.length;i++){//遍历所有深水炸弹
            if (bombs[i].isOutofBounds() || bombs[i].isDead()){
                bombs[i] = bombs[bombs.length-1];
                bombs = Arrays.copyOf(bombs,bombs.length-1);
            }
        }
    }



    private int score = 0;
    public void bombBangAction(){
        for (int i=0;i<bombs.length;i++){
            Bomb b = bombs[i];
            for (int j=0;j<submarines.length;j++){
                SeaObject s = submarines[j];
                if (b.isLive() && s.isLive() && s.isHit(b)){
                    s.goDead();
                    b.goDead();
                    if (s instanceof EnemScore){//如果被撞潜艇  为分
                        EnemScore es = (EnemScore) s;//将被撞潜艇强转为得分接口
                        score += es.getScore();//玩家得分
                    }
                    if (s instanceof EnemLife){//如果被撞潜艇   为命
                        EnemLife el = (EnemLife) s;//将被撞潜艇强转为得命接口
                        int num = el.getLife();//获取el里的命数值 传给num中
                        ship.addLife(num);//将数值传给Battleship里addLife方法
                    }
                }
            }
        }
    }
    public void mineBangAction(){
        for (int i=0;i<mines.length;i++){//遍历所有炸弹数组
            Mine  m= mines[i];//把遍历到的那位数组赋值给炸弹
            if (m.isLive() && ship.isLive() && m.isHit(ship)){//如果炸弹活着并且战舰也活着
                m.goDead();//炸弹去死
                ship.subtraction();//战舰减命
            }
        }
    }

    public void chackGameoverAction(){
        if (ship.getLife() <= 0){//如果战舰命数小于等于0
            state = GAME_OVER;
        }
    }//游戏结束

    //启动程序的执行
    public void action(){
        KeyAdapter k = new KeyAdapter() {
            //重写keyReleassed()按键抬起事件
            public void keyReleased(KeyEvent e) {//当按键抬起是自动执行
                if (e.getKeyCode()==KeyEvent.VK_SPACE){//若按键是空格键
                    Bomb obj = ship.shootBomb();//获取战舰对象
                    bombs = Arrays.copyOf(bombs,bombs.length+1);
                    bombs[bombs.length-1] = obj;//将obj添加到数组的最后一个元素上
                }
                if(e.getKeyCode()==KeyEvent.VK_LEFT){//若按键是left则执行
                    ship.moveLeft();
                }
                if (e.getKeyCode()==KeyEvent.VK_RIGHT){//若按键是right则执行
                    ship.moveRight();
                }
            }
        };
        this.addKeyListener(k);//添加侦听

        System.out.println("添加侦听");
        Timer timer = new Timer();//定时器对象
        int interval = 10;//定时器间隔（以毫秒为单位）
        timer.schedule(new TimerTask() {
            public void run() {//定义干的事
                submarineEnterAction();//三个潜艇
                mineEnterAction();
                moveAction();
                outOfBoundsAction();//删除越界对象
                bombBangAction();//深水炸弹与潜艇碰撞
                mineBangAction();
                chackGameoverAction();//结束游戏
                repaint();//重新画
            }
        },interval,interval);
    }




    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World word =new World();
        word.setFocusable(true);
        frame.add(word);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+16,HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        word.action();
        //侦听器调用

    }
}
